package com.oymn.myapplication.page;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.oymn.myapplication.MainActivity;
import com.oymn.myapplication.R;
import com.oymn.myapplication.utils.Url;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class register extends AppCompatActivity {

    Button queding = null;
    TextView username = null;
    TextView password = null;

    protected static final int REGISTER_SUCCESS = 0;
    protected static final int REGISTER_FAIL = 1;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case REGISTER_SUCCESS:
                    //如果注册成功的话，跳转到登录界面
                    Toast.makeText(register.this,"注册成功",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(register.this, MainActivity.class);
                    startActivity(intent);
                    break;
                case REGISTER_FAIL:
                    //登录失败
                    Toast.makeText(register.this, "用户名已存在", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);

        queding = findViewById(R.id.queding);
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register(username.getText().toString(),password.getText().toString());
            }
        });
    }

    public void register(String username,String password){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost request = new HttpPost(Url.user_url);

                //封装参数
                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("action","register"));
                param.add(new BasicNameValuePair("user_phone",username));
                param.add(new BasicNameValuePair("user_password",password));

                try {
                    request.setEntity(new UrlEncodedFormEntity(param));
                    //获取响应的结果
                    HttpResponse response = httpClient.execute(request);
                    HttpEntity entity = response.getEntity();
                    //获取响应结果的信息
                    String result = EntityUtils.toString(entity,"UTF-8");
                    if("register success".equals(result)){
                        Message msg = new Message();
                        msg.what = REGISTER_SUCCESS;
                        handler.sendMessage(msg);
                    }
                    else if("phone already exist".equals(result)){
                        Message msg = new Message();
                        msg.what = REGISTER_FAIL;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
